﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RoleAuthenticate.aspx.cs" Inherits="Admin_RoleAuthenticate" StylesheetTheme="ContentMS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body style="margin:0;" bgcolor="#D8E7FA">
    <form id="form1" runat="server">
    <table class="TableCss" bgcolor="#D8E7FA" width="100%" cellpadding="0" cellspacing="0">
		<tr>
			<td background="../Images/HT_top.jpg" colspan="2" height="25">
				<table height="25" cellpadding="0" cellspacing="0" border="0">
					<tr>
						<td height="25" valign="middle" align="center">&nbsp;&nbsp;&nbsp;<img src="../Images/HT_top_ioc.GIF" /></td>
						<td><font class="AdminTitle">&nbsp;&nbsp;<b>设置角色的权限</b></font></td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td align="left">角色名称：<font color="red"><asp:Label ID="lbName" runat="server"></asp:Label></font></td>
		</tr>	
		<tr>
			<td style="border-style:solid;" valign="top">
				<asp:TreeView ID="CatalogView" runat="server" Width="100%" SkinID="girdviewskin" ShowCheckBoxes="All" OnSelectedNodeChanged="CatalogView_SelectedNodeChanged" OnTreeNodeCheckChanged="CatalogView_TreeNodeCheckChanged" ShowLines="True">
				</asp:TreeView>
			</td>			
		</tr>
		<tr>
			<td align="left" colspan="2" style="height: 22px">
			  <asp:Button ID="btnSetAuth" runat="server" CssClass="ButtonCss" Text="设置该角色的权限" CommandName="set" OnCommand="Btn_Command" Width="145px" CausesValidation="False"  />&nbsp;&nbsp;
			  <asp:Button ID="ReturnBtn" runat="server" CssClass="ButtonCss" Text="返回" CausesValidation="False" CommandName="return" OnCommand="Btn_Command" Width="80px"  />
				<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></td>
		</tr>		
    </table>
    <script language="javascript" type="text/javascript">
	//节点的oncheck事件
	function TreeView_OnCheck(tree)
	{
		var node = tree.getTreeNode(tree.clickedNodeIndex);
		var Pchecked = tree.getTreeNode(tree.clickedNodeIndex).getAttribute("Checked");
		setcheck(node,Pchecked);
		setParent(node,Pchecked);//如果是选择那么选择其父接点.
		document.all.checkedNodes.value="";
		document.all.unchecked.value="";
		FindCheckedFromNode(CatalogView);
	}

	//设置子节点选中/取消;
	//同时需要设置父节点的状态(如果是取消选中的话,仅仅设置本节点及其所有字接点,不涉及父接点)
	function setParent(node,Pc)
	{
		var parentNode = node.getParent();
		if(parentNode)
		{    
			var parentNodeFather = parentNode.getParent();
			if(parentNodeFather)
			{
				setParent(parentNode,Pc);
			}
			if(Pc)
				{parentNode.setAttribute("Checked",Pc);}
			else
			{
				checkBrother(parentNode,Pc,node.getAttribute("Value"))
			}
		}
	}
	//检查子接点是否有选择的,如果有一个选择了,那返回true
	//只查第一级节点.
	function checkBrother(parentNode,Pc,nodeData)
	{
		var childNodes = new Array();
		childNodes = parentNode.getChildren();
		if(childNodes.length >0)
		{
			var bChecked = true;
			for(var i=0;i<childNodes.length;i++)
			{
				if(childNodes[i].getAttribute("Checked") == true &&  childNodes[i].getAttribute("Value") != nodeData)
				{
					//alert(i+childNodes[i].getAttribute("Text"));
					bChecked = false;
					break;
				}
			}
			if(bChecked)
			{
				parentNode.setAttribute("Checked",Pc);
			}
			else
			{
				//所有父结点选择
				setParent(parentNode,!Pc)
			}
		}
		else
		{
			parentNode.setAttribute("Checked",Pc);
		}
	}
	//获取所有节点状态
    function FindCheckedFromNode(node) 
    {
        var i = 0;
        var nodes = new Array();
        nodes = node.getChildren();
         
        for (i = 0; i < nodes.length; i++) 
        {
            var cNode;
            cNode=nodes[i];
            if (cNode.getAttribute("Checked"))
                AddChecked(cNode);
            else
                AddUnChecked(cNode);
              
            if (parseInt(cNode.getChildren().length) != 0 ) 
            {
                FindCheckedFromNode(cNode);
            }
        }
    }
	//添加选中节点
    function AddChecked(node) 
    {
        document.all.checkedNodes.value += node.getAttribute("Value");
        document.all.checkedNodes.value += ',';
    }
    //添加未选中节点
    function AddUnChecked(node)
    {
        document.all.unchecked.value += node.getAttribute("Value");
        document.all.unchecked.value += ',';
    }

    //设置子节点选中
	function setcheck(node,Pc)
	{
		var i;
		var ChildNode = new Array();
		ChildNode = node.getChildren();
         
		if(parseInt(ChildNode.length)==0)
			return;
		else
		{
			for(i=0;i<ChildNode.length;i++)
			{
				var cNode;
				cNode=ChildNode[i];
				if(parseInt(cNode.getChildren().length)!=0)
					setcheck(cNode,Pc);
				cNode.setAttribute("Checked",Pc);
			}
		}
	}
    </script>
    </form>
</body>
</html>
